<template>
    <div class="login">
        <el-card class="card">
            <template #header>
                <h3>用户登录</h3>
            </template>
            <el-form :rules="rules" ref="loginForm" :model="user">
                <el-form-item label="账号" prop="account">
                    <el-input class="input" v-model="user.account" placeholder="请输入账号" clearable />
                </el-form-item>
                <el-form-item label=" 密码" prop="password">
                    <el-input class="input" v-model="user.password" type="password" placeholder="请输入密码" clearable
                        show-password />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="footer">
                    <el-button class="login-btn" type="primary" @click="login">登录</el-button>
                    <el-text class="mx-1" type="info" @click="$router.push('/register')">还没有账号？去注册</el-text>
                </div>
            </template>
        </el-card>
    </div>
</template>

<script>
import { ElMessage } from "element-plus";
import { LoginAPI } from "@/api/user";
export default {
    data() {
        return {
            user: {
                account: '',
                password: '',
            },
            rules: {
                account: { required: true, message: "请输入账号", trigger: "blur" },
                password: { required: true, message: "请输入密码", trigger: "blur" }
            },
        };
    },
    methods: {
        login() {
            this.$refs.loginForm.validate((val) => {
                if (val) {
                    LoginAPI(this.user).then((resp) => {
                        if (resp.data.code == 2000) {
                            ElMessage.success(resp.data.message);
                            sessionStorage.setItem('user', JSON.stringify(resp.data.data));
                            this.$router.push('/');
                        } else {
                            ElMessage.error(resp.data.message);
                        }
                    })
                }
            });
        },
    },
};
</script>

<style scoped>
.login {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    background-image: url("@/assets/imgs/居民登录注册背景图.jpg");
    background-repeat: no-repeat;
    background-size: cover;
    background-position: 0px, 0px;
}

.login .card {
    width: 400px;
    max-width: 90vw;
    opacity: 0.90;
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

@media (max-width: 768px) {
    .login {
        background-position: center;
    }

    .login .card {
        width: 95vw;
        max-width: 400px;
    }
}

.login .card .input {
    width: 250px;
}

.footer {
    width: 300px;
}

.footer .login-btn {
    width: 100%;
}

.footer .mx-1 {
    cursor: pointer;

    &:hover {
        color: #409EFF;
    }
}
</style>
